\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\graphicspath{ {out/} }
\usepackage{subcaption}
\usepackage[export]{adjustbox}
\usepackage{wrapfig}

\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
backgroundcolor=\color{backcolour}, 
basicstyle=\ttm,
otherkeywords={self,assert},             % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tbrl,                         % Any extra options here
showstringspaces=false,            % 
numbers=left,                    
numbersep=5pt,    
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}






\begin{document}
  \title{Digital Image Processing Homework Report \#1}
  \author{Ling Dai, 116033910029}
  \maketitle
  
  \section{Description}
    Histogram equalization is a method in image processing of 
    contrast adjustment using the image's histogram. This method 
    usually increases the global contrast of many images, 
    especially when the usable data of the image is represented 
    by close contrast values. Through this adjustment, the 
    intensities can be better distributed on the histogram. 
    This allows for areas of lower local contrast to gain a higher
    contrast. Histogram equalization accomplishes this by 
    effectively spreading out the most frequent intensity values.
    
    In this assignment, three tasks are to be finished:
    \begin{itemize}
      \item Write a computer program for computing the histogram 
      of an image.
      \item Implemente the histogram equalization technique.
    \end{itemize}
    
  \section{Implementation}
    \subsection{Layout}
    This assignment was implemented using python, source code  of this assignment can be found in following files
    \begin{quote}
     dipmod/histogram.py \\
     hw1/main.py
    \end{quote} 
    Where the main.py file are the entrance of the implementation, and histogram.py implemented histogram computing algorithm and histogram equalization algorithm.
    \subsection{Dependency}
    This project were tested and run under python3.5 on Ubuntu 16.04 OS, and depend on these python libraries:
    \begin{quote}
      scipy\\
      numpy\\
      matplotlib
    \end{quote}
    Use apt-get install python3-package\_name to install the corresponding packages.

    \subsection{Histogram calculation}
      An image histogram is a type of histogram that acts as a 
      graphical representation of the tonal distribution in a 
      digital image. It plots the number of pixels for each 
      tonal value. The main code of the algorithm are listed below in Algorithm.\ref{algorithm:1}, the code can also be found in file {\it histogram.py}.
      
      \begin{python}[caption=Histogram computing,label=algorithm:1]
def hist_calculate(img, scale=256):
    assert img.ndim == 2
    hist = np.array([0] * scale)
    scale -= 1
    for pixel in np.nditer(img):
	index = int(pixel * scale)
	hist[index] += 1
    hist = hist / hist.sum()
    return hist
	\end{python}

    The input of this function is a gray scale image in 2-dimension array format and number of bins to perform the histogram computation with a default value of 256, and the output is corresponding histogram of the image.
    
    \subsection{Histogram equalization}
    For histogram equalization, we need to calculate the PDF of original image, this is finished based on the histogram of image, and then a pixel-wise transformation is performed on original image to obtain the histogram equalized image.
    
    The  core code are listed in Algorithm.\ref{algorithm:2}
    \begin{python}[label=algorithm:2, caption=Histogram equalization algorithm]
def hist_adjust(img, hist):
    assert img.ndim == 2
    assert hist.ndim == 1
    img = img.copy()
    hist = hist.copy()
    N = hist.shape[0] - 1
    for i in range(1,hist.shape[0]):
        hist[i] += hist[i-1]
    for pixel in np.nditer(img, op_flags=['readwrite']):
        index = int(pixel * N)
        pixel[...] = hist[index]
    return img     
    \end{python}
    
    The input is an image, the same format as previous function, and a histogram result produced by previous function. Line 7\~8 calculate the PDF of image, and following lines perform the pixel-wise transformation.

  \section{Result}
  The algorithm are tested on two images as required. Figure.\ref{figure:result1}-\ref{figure:result2} shows the result of this algorithm.
    
  \begin{figure}[h]
    \label{figure:result1}
    \caption{Result of histogram equalization of Fig2}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{Fig2_original_img.jpg} 
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{Fig2_hist_equalized_img.jpg} 
    \end{subfigure}

    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.99\linewidth]{Fig2_original_hist.pdf} 
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.99\linewidth]{Fig2_hist_equalized_hist.pdf} 
    \end{subfigure}
  \end{figure}
    
  \begin{figure}[h]
    \label{figure:result1}
    \caption{Result of histogram equalization of Fig1}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{Fig1_original_img.jpg} 
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{Fig1_hist_equalized_img.jpg} 
    \end{subfigure}

    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.99\linewidth]{Fig1_original_hist.pdf} 
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.99\linewidth]{Fig1_hist_equalized_hist.pdf} 
    \end{subfigure}
  \end{figure}
  
    

    
    

\end{document}


















